{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get class-pid-index map for synapse abdominal CT dataset "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Once deleted, variables cannot be recovered. Proceed (y/[n])? y\n",
      "The autoreload extension is already loaded. To reload it, use:\n",
      "  %reload_ext autoreload\n"
     ]
    }
   ],
   "source": [
    "%reset\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "import numpy as np\n",
    "import os\n",
    "import glob\n",
    "import SimpleITK as sitk\n",
    "import matplotlib.pyplot as plt\n",
    "import sys\n",
    "sys.path.insert(0, '../../dataloaders/')\n",
    "import niftiio as nio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# lets save them in a same way as mmwhs for the ease of modifying dataloader\n",
    "\n",
    "# normalization: cut top 2% of histogram, then doing volume-wise normalization\n",
    "\n",
    "IMG_BNAME=\"./sabs_CT_normalized/image_*.nii.gz\"\n",
    "SEG_BNAME=\"./sabs_CT_normalized/label_*.nii.gz\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "imgs = glob.glob(IMG_BNAME)\n",
    "segs = glob.glob(SEG_BNAME)\n",
    "imgs = [ fid for fid in sorted(imgs, key = lambda x: int(x.split(\"_\")[-1].split(\".nii.gz\")[0])  ) ]\n",
    "segs = [ fid for fid in sorted(segs, key = lambda x: int(x.split(\"_\")[-1].split(\".nii.gz\")[0])  ) ]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['./sabs_CT_normalized/image_0.nii.gz',\n",
       " './sabs_CT_normalized/image_1.nii.gz',\n",
       " './sabs_CT_normalized/image_2.nii.gz',\n",
       " './sabs_CT_normalized/image_3.nii.gz',\n",
       " './sabs_CT_normalized/image_4.nii.gz',\n",
       " './sabs_CT_normalized/image_5.nii.gz',\n",
       " './sabs_CT_normalized/image_6.nii.gz',\n",
       " './sabs_CT_normalized/image_7.nii.gz',\n",
       " './sabs_CT_normalized/image_8.nii.gz',\n",
       " './sabs_CT_normalized/image_9.nii.gz',\n",
       " './sabs_CT_normalized/image_10.nii.gz',\n",
       " './sabs_CT_normalized/image_11.nii.gz',\n",
       " './sabs_CT_normalized/image_12.nii.gz',\n",
       " './sabs_CT_normalized/image_13.nii.gz',\n",
       " './sabs_CT_normalized/image_14.nii.gz',\n",
       " './sabs_CT_normalized/image_15.nii.gz',\n",
       " './sabs_CT_normalized/image_16.nii.gz',\n",
       " './sabs_CT_normalized/image_17.nii.gz',\n",
       " './sabs_CT_normalized/image_18.nii.gz',\n",
       " './sabs_CT_normalized/image_19.nii.gz',\n",
       " './sabs_CT_normalized/image_20.nii.gz',\n",
       " './sabs_CT_normalized/image_21.nii.gz',\n",
       " './sabs_CT_normalized/image_22.nii.gz',\n",
       " './sabs_CT_normalized/image_23.nii.gz',\n",
       " './sabs_CT_normalized/image_24.nii.gz',\n",
       " './sabs_CT_normalized/image_25.nii.gz',\n",
       " './sabs_CT_normalized/image_26.nii.gz',\n",
       " './sabs_CT_normalized/image_27.nii.gz',\n",
       " './sabs_CT_normalized/image_28.nii.gz',\n",
       " './sabs_CT_normalized/image_29.nii.gz']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "imgs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(85, 257, 257)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lb = nio.read_nii_bysitk(segs[0])\n",
    "lb.shape\n",
    "# please check the organizations of dimensions. We will iterate through the z dimension.\n",
    "# it should keep consistent with those for CHAOS dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pid 0 finished!\n",
      "pid 1 finished!\n",
      "pid 2 finished!\n",
      "pid 3 finished!\n",
      "pid 4 finished!\n",
      "pid 5 finished!\n",
      "pid 6 finished!\n",
      "pid 7 finished!\n",
      "pid 8 finished!\n",
      "pid 9 finished!\n",
      "pid 10 finished!\n",
      "pid 11 finished!\n",
      "pid 12 finished!\n",
      "pid 13 finished!\n",
      "pid 14 finished!\n",
      "pid 15 finished!\n",
      "pid 16 finished!\n",
      "pid 17 finished!\n",
      "pid 18 finished!\n",
      "pid 19 finished!\n",
      "pid 20 finished!\n",
      "pid 21 finished!\n",
      "pid 22 finished!\n",
      "pid 23 finished!\n",
      "pid 24 finished!\n",
      "pid 25 finished!\n",
      "pid 26 finished!\n",
      "pid 27 finished!\n",
      "pid 28 finished!\n",
      "pid 29 finished!\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "classmap = {}\n",
    "LABEL_NAME = [\"BGD\", \"SPLEEN\", \"KID_R\", \"KID_l\", \"GALLBLADDER\", \"ESOPHAGUS\", \"LIVER\", \"STOMACH\", \"AORTA\", \"IVC\", \"PS_VEIN\", \"PANCREAS\", \"AG_R\", \"AG_L\"]     \n",
    "\n",
    "MIN_TP=1 # minimum number of true positive pixels in a slice\n",
    "\n",
    "fid = f'./sabs_CT_normalized/classmap_{MIN_TP}.json'\n",
    "for _lb in LABEL_NAME:\n",
    "    classmap[_lb] = {}\n",
    "    for pid in range(len(segs)):\n",
    "        classmap[_lb][str(pid)] = []\n",
    "\n",
    "for pid, seg in enumerate(segs):\n",
    "    lb_vol = nio.read_nii_bysitk(seg)\n",
    "    n_slice = lb_vol.shape[0]\n",
    "    for slc in range(n_slice):\n",
    "        for cls in range(len(LABEL_NAME)):\n",
    "            if cls in lb_vol[slc, ...]:\n",
    "                if np.sum( lb_vol[slc, ...] == cls) >= MIN_TP:\n",
    "                    classmap[LABEL_NAME[cls]][str(pid)].append(slc)\n",
    "    print(f'pid {str(pid)} finished!')\n",
    "    \n",
    "with open(fid, 'w') as fopen:\n",
    "    json.dump(classmap, fopen)\n",
    "    fopen.close()  \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
